package com.devdan.headsetfix;

import static com.devdan.headsetfix.HeadsetCommonConstants.*;
import static com.devdan.headsetfix.HeadsetCommonConstants.KEY_TRIAL_MONTH;
import static com.devdan.headsetfix.HeadsetCommonConstants.KEY_TRIAL_YEAR;
import static com.devdan.headsetfix.HeadsetCommonConstants.NUMBER_OF_TRIAL_DAYS;
import static com.devdan.headsetfix.HeadsetCommonConstants.PREFSFILE;
import static com.devdan.headsetfix.HeadsetCommonConstants.TRIAL_DATE_NOT_YET_SET;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

public class TrialChecker {
	
	public boolean isTrialPeriodExceeded(Context context) {
		return (trialExceededCheckInternalMemory(context) || trialExceededCheckExternalMemory(context));
	}
	
	private boolean trialExceededCheckExternalMemory(Context context) {
		String eol = System.getProperty("line.separator");
		Calendar todaysDate = Calendar.getInstance(); 
		int currentYear = todaysDate.get(Calendar.YEAR) ^ OBFUSCATE_FACTOR_1;		// Obfuscate a little bit
		int currentMonth = todaysDate.get(Calendar.MONTH) ^ OBFUSCATE_FACTOR_2;		
		int currentDay = todaysDate.get(Calendar.DAY_OF_MONTH) ^ OBFUSCATE_FACTOR_3;			
		if (!isSDCardWriteable()) {
			return false;
		}
		// Create a path where we will place our private file on external
	    // storage.
	    File file = new File("/sdcard/", TRIAL_FILE_NAME);
	    if (!file.exists()) {
	    	try {
	    		BufferedWriter writer = new BufferedWriter(new FileWriter(file)); 		
	    		writer.write(currentYear + eol);
	    		writer.write(currentMonth + eol);
	    		writer.write(currentDay + eol);
	    		writer.close();
	    	}
	    	catch (IOException e){
	    	}
	    	return false;
	    }
	    else {
			Calendar storedDate = Calendar.getInstance();
	    	try {
				BufferedReader input = new BufferedReader(new FileReader(file));
				storedDate.set(Calendar.YEAR,Integer.parseInt(input.readLine()) ^ OBFUSCATE_FACTOR_1);			
				storedDate.set(Calendar.MONTH,Integer.parseInt(input.readLine()) ^ OBFUSCATE_FACTOR_2);			
				storedDate.set(Calendar.DAY_OF_MONTH,Integer.parseInt(input.readLine()) ^ OBFUSCATE_FACTOR_3);
				input.close();
				long differenceInDays = daysBetween(storedDate,todaysDate);
				if (differenceInDays > NUMBER_OF_TRIAL_DAYS) {
					return true;
				}
				return false;		
				
			} catch (Exception e) {
				return true;
			}	    	
	    }
	
	}

	private boolean isSDCardWriteable() {
		boolean isSDWriteable = false;
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
			isSDWriteable  = true;
		} 
		return isSDWriteable;
	}

	private boolean trialExceededCheckInternalMemory(Context context) {
		SharedPreferences settings = context.getSharedPreferences(PREFSFILE, 0);       
		SharedPreferences.Editor settingsEditor = settings.edit();
		Calendar todaysDate = Calendar.getInstance(); 
		int currentYear = todaysDate.get(Calendar.YEAR);		
		int currentMonth = todaysDate.get(Calendar.MONTH);		
		int currentDay = todaysDate.get(Calendar.DAY_OF_MONTH);		
		int storedYear = settings.getInt(KEY_TRIAL_YEAR , TRIAL_DATE_NOT_YET_SET);
		if (storedYear == TRIAL_DATE_NOT_YET_SET) {
    		settingsEditor.putInt(KEY_TRIAL_YEAR, currentYear);
    		settingsEditor.putInt(KEY_TRIAL_MONTH, currentMonth);
    		settingsEditor.putInt(KEY_TRIAL_DAY, currentDay);
    		settingsEditor.commit(); 		
    		return false;
		}
		
		int storedMonth = settings.getInt(KEY_TRIAL_MONTH , TRIAL_DATE_NOT_YET_SET);
		int storedDay = settings.getInt(KEY_TRIAL_DAY , TRIAL_DATE_NOT_YET_SET);
		Calendar storedDate = Calendar.getInstance();
		storedDate.set(Calendar.DAY_OF_MONTH, storedDay);
		storedDate.set(Calendar.MONTH,storedMonth);
		storedDate.set(Calendar.YEAR, storedYear);			
		long differenceInDays = daysBetween(storedDate,todaysDate);
		if (differenceInDays > NUMBER_OF_TRIAL_DAYS) {
			return true;
		}
		return false;		
	}

	public static long daysBetween(Calendar startDate, Calendar endDate) {
		  Calendar date = (Calendar) startDate.clone();
		  long daysBetween = 0;
		  while (date.before(endDate)) {
		      date.add(Calendar.DAY_OF_MONTH, 1);
		      daysBetween++;
		  }
		  return daysBetween;
		}

}